﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Might.UIControls.FileSystemBrowser {
    public partial class LoginView : ILoginView {
        private bool logged;

        public event EventHandler Login;
        public event EventHandler LoginAnonymous;
        public event EventHandler Logout;

        public LoginView() {
            InitializeComponent();
        }

        public new string Name {
            get { return UserNameTxt.Text; }
        }

        public string Pass {
            get { return PasswordTxt.Password; }
        }

        public bool Logged {
            set {
                if (value) {
                    //LoginAnonymousBtn.Visibility = Visibility.Collapsed;
                    LoginBtn.Content = "Logout";
                    UserNameTxt.IsEnabled = false;
                    PasswordTxt.IsEnabled = false;                   
                } else {
                    LoginBtn.Content = "Login";
                    UserNameTxt.IsEnabled = true;
                    PasswordTxt.IsEnabled = true;
                    //LoginAnonymousBtn.Visibility = Visibility.Visible;
                }
                IsEnabled = true;
                logged = value;
            }
            get { return logged; }
        }

        protected void onLogin(object sender, EventArgs e) {
            if (Login != null) {
                IsEnabled = false;
                Login(this, e);
            }
        }

        protected void onLoginAnonymous(object sender, EventArgs e) {
            if (LoginAnonymous != null) {
                IsEnabled = false;
                LoginAnonymous(this, e);
            }
        }

        protected void onLogout(object sender, EventArgs e) {
            if (Logout != null) {
                IsEnabled = false;
                Logout(this, e);
            }
        }

        private void LoginBtn_Click(object sender, RoutedEventArgs e) {
            if (logged) {
                onLogout(this, e);
            } else {
                onLogin(this, e);
            }
        }

        private void LoginAnonymousBtn_Click(object sender, RoutedEventArgs e) {
            UserNameTxt.Text = "Anonymous";
            PasswordTxt.Password = "";
            onLoginAnonymous(this, e);
        }
        
    }
}
